package com.bocloud.cmp.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.bocloud.cmp.dao.ClusterDao;
import com.bocloud.cmp.entity.resource.Cluster;
import com.bocloud.common.model.Param;
import com.bocloud.common.model.SimpleBean;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.MapTools;
import com.bocloud.database.core.intf.impl.JdbcGenericDao;
import com.bocloud.database.utils.SQLHelper;

/**
 * 集群数据处理接口实现
 * 
 * @author weiwei
 * @version 1.0
 * @since 2016.12.28
 *
 */
@Component("clusterDao")
public class ClusterDaoImpl extends JdbcGenericDao<Cluster, Long> implements ClusterDao {

	@Override
	public List<Cluster> list(int page, int rows, List<Param> params, Map<String, String> sorter) throws Exception {
		String sql = "select a.*,b.name vdc_name from cluster a left join vdc b on a.vdc_id = b.id where a.is_deleted = 0";
		sql = SQLHelper.buildRawSql(sql, page, rows, params, sorter, "a");
		Map<String, Object> paramMap = SQLHelper.getParam(params);
		return this.list(Cluster.class, sql, paramMap);
	}

	@Override
	public List<SimpleBean> list(List<Param> params, Map<String, String> sorter) throws Exception {
		String sql = "select a.id,a.name from cluster a where a.is_deleted = 0";
		sql = SQLHelper.buildSql(sql, 1, Integer.MAX_VALUE, params, sorter, "a");
		Map<String, Object> paramMap = SQLHelper.getParam(params);
		List<Cluster> clusters = this.list(Cluster.class, sql, paramMap);
		List<SimpleBean> beans = new ArrayList<SimpleBean>();
		for (Cluster cluster : clusters) {
			beans.add(new SimpleBean(cluster.getId(), cluster.getName()));
		}
		return beans;
	}

	@Override
	public boolean remove(Long id, Long userId) throws Exception {
		String sql = "update cluster set is_deleted = true , gmt_modify = :gmtModify ,mender_id = :menderId where is_deleted = 0 and id = :id";
		Map<String, Object> params = MapTools.simpleMap("id", id);
		params.put("menderId", userId);
		params.put("gmtModify", new Date());
		return this.execute(sql, params) > 0;
	}

	@Override
	public int count(List<Param> params) throws Exception {
		String sql = "select count(1) from cluster a where a.is_deleted = 0";
		sql = SQLHelper.buildRawSql(sql, params, null, "a");
		Map<String, Object> paramMap = SQLHelper.getParam(params);
		return this.countQuery(sql, paramMap).intValue();
	}

	@Override
	public Cluster query(Long id) throws Exception {
		String sql = "select a.*,b.name vdc_name from cluster a left join vdc b on a.vdc_id = b.id where a.is_deleted = 0 and a.id = :id";
		Map<String, Object> param = MapTools.simpleMap(Common.ID, id);
		List<Cluster> list = this.list(Cluster.class, sql, param);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	@Override
	public boolean removeByVendor(Long vendorId, Long userId) throws Exception {
		String sql = "update cluster set is_deleted = true , gmt_modify = :gmtModify ,mender_id = :menderId where is_deleted = 0 and vendor_id = :vendorId";
		Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
		params.put("menderId", userId);
		params.put("gmtModify", new Date());
		return this.execute(sql, params) > 0;
	}

	@Override
	public List<Cluster> listByVdc(Long vdcId) throws Exception {
		String sql = "select a.* from cluster a where a.is_deleted = 0 and a.vdc_id = :vdcId";
		Map<String, Object> params = MapTools.simpleMap("vdcId", vdcId);
		return this.list(Cluster.class, sql, params);
	}

	@Override
	public List<Cluster> listByVendor(Long vendorId) throws Exception {
		String sql = "select a.* from cluster a where a.is_deleted = 0 and a.vendor_id = :vendorId";
		Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
		return this.list(Cluster.class, sql, params);
	}

	@Override
	public boolean removeByVdc(Long vdcId, Long userId) throws Exception {
		String sql = "update cluster set is_deleted = true , gmt_modify = :gmtModify ,mender_id = :menderId where is_deleted = 0 and vdc_id = :vdcId";
		Map<String, Object> params = MapTools.simpleMap("vdcId", vdcId);
		params.put("menderId", userId);
		params.put("gmtModify", new Date());
		return this.execute(sql, params) > 0;
	}

}
